package com

import com.sun.xml.internal.org.jvnet.mimepull.Data

class VendaController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		redirect(action: "create", params: params)
	}

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[vendaInstanceList: Venda.list(params), vendaInstanceTotal: Venda.count()]
	}

	def create = {
		def vendaInstance = new Venda()
		vendaInstance.properties = params
		return [vendaInstance: vendaInstance]
	}

	def save = {
		def vendaInstance = new Venda(params)
		
		if (vendaInstance.parcelas < 0 && vendaInstance.valor <= 0) {
			render(view: "create", model: [vendaInstance: vendaInstance])
			flash.message = "Valores invalidos!"
			return
		}
		if (vendaInstance.save(flush: true)) {
			println("SAVE -> params:  "+ params)
			int i = 1
			
			//criando contas a receber
			Date date = new Date()
			
			int parcela = Integer.parseInt(params.parcelas)
				
			double valorParcela = vendaInstance.valor /vendaInstance.parcelas
			
			if (! vendaInstance.entrada){
				println ("sem entrada somar mais 30"+ date)
				date = date + 30
			}
			
			while (i <= parcela) {  
				geraContaReceber(vendaInstance, date, valorParcela)
				 i = i +1
				date = date + 30
			}
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'venda.label', default: 'Venda'), vendaInstance.id])}"
			redirect(action: "show", id: vendaInstance.id)
		}
		else {
			render(view: "create", model: [vendaInstance: vendaInstance])
		}
	}

	def show = {
		def vendaInstance = Venda.get(params.id)
		if (!vendaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
			redirect(action: "list")
		}
		else {
			[vendaInstance: vendaInstance]
		}
	}

	def edit = {
		def vendaInstance = Venda.get(params.id)
		if (!vendaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [vendaInstance: vendaInstance]
		}
	}

	def update = {
		def vendaInstance = Venda.get(params.id)
		if (vendaInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (vendaInstance.version > version) {

					vendaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'venda.label', default: 'Venda')]
					as Object[], "Another user has updated this Venda while you were editing")
					render(view: "edit", model: [vendaInstance: vendaInstance])
					return
				}
			}
			vendaInstance.properties = params
			if (!vendaInstance.hasErrors() && vendaInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'venda.label', default: 'Venda'), vendaInstance.id])}"
				redirect(action: "show", id: vendaInstance.id)
			}
			else {
				render(view: "edit", model: [vendaInstance: vendaInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		def vendaInstance = Venda.get(params.id)
		if (vendaInstance) {
			try {
				vendaInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'venda.label', default: 'Venda'), params.id])}"
			redirect(action: "list")
		}
	}
	public void geraContaReceber(Venda venda, Date date, double valorParcela){
		println ("gerando contas a receber")
		println ("Date"+ date)	
		ContasReceber contasReceber = new ContasReceber()
	    contasReceber.valor = valorParcela
		contasReceber.venda = venda
		contasReceber.dataVencimento = date
		//contasReceber.dataBaixa = date + 30
		contasReceber.save()
	}
}
